import turtle
import math
from turtleGameScreenWriter import *
class IssacNLilithGame:
    scoreWriter=turtleGameScreenWriter()
    # define an erasable

    turtle.register_shape("1.gif")
    turtle.register_shape("2.gif")
    Issac = turtle.Turtle("2.gif")
    lilith = turtle.Turtle("1.gif")
    bean=turtle.Turtle("circle")
    IssacScore=0
    lilithScore=0
    beanPicked= False
    #turtle.hideturtle()
    #turtle.tracer(False)
    def __init__(self):
        self.lilith.penup()
        self.Issac.penup()
        self.bean.penup()
        self.bean.goto(-200, 200)
        self.Issac.forward(300)
        self.lilith.forward(100)
    def moveRight(self):
        print("d pressed:")
        dist=self.distance(self.lilith.pos(),self.bean.pos())
        print(dist)
        if dist < 66.0:
            if not self.beanPicked:
                    self.lilithScore+=1
                    self.beanPicked = True
                    self.bean.hideturtle()
        print("LilithScore ", self.lilithScore)
        #self.scoreWriter.write("LilithScore "+str(self.lilithScore))
        self.scoreWriter.write("LilithScore " + str(self.lilithScore), (-150, -150))
        self.lilith.forward(50)
    def moveUp(self):
        print("w pressed:")
        dist = self.distance(self.lilith.pos(), self.bean.pos())
        print(dist)
        if dist < 66.0:
            if not self.beanPicked:
                    self.lilithScore+=1
                    self.beanPicked = True
                    self.bean.hideturtle()
        print("LilithScore ", self.lilithScore)
        #self.scoreWriter.write("LilithScore " + str(self.lilithScore))
        self.scoreWriter.write("LilithScore " + str(self.lilithScore), (-150, -150))
        # use erasable to update score on screen


        self.lilith.left(90)
        self.lilith.forward(50)
    def IssacMoveRight(self):
        print("6 pressed:")
        dist = self.distance(self.Issac.pos(), self.bean.pos())
        print(dist)
        if dist < 66.0:
            if not self.beanPicked:
                self.IssacScore += 1
                self.beanPicked = True
                self.bean.hideturtle()
        print("IssacScore ", self.IssacScore)
        self.scoreWriter.write("IssacScore " + str(self.IssacScore), (-150, 150))
        self.Issac.forward(50)

    def IssacMoveLeft(self):
        print("4 pressed:")
        dist = self.distance(self.Issac.pos(), self.bean.pos())
        print(dist)
        if dist < 66.0:
            if not self.beanPicked:
                self.IssacScore += 1
                self.beanPicked = True
                self.bean.hideturtle()
        print("IssacScore ", self.IssacScore)
        self.scoreWriter.write("IssacScore " + str(self.IssacScore),(-150,150))
        self.Issac.left(180)
        self.Issac.forward(50)
    def distance(self,co1, co2):
        return math.sqrt(pow(abs(co1[0]-co2[0]),2 ) + pow(abs(co1[1]-co2[1]), 2))

    def gameRun(self):
        turtle.listen()
        turtle.onkeypress(self.moveRight,"d" )
        turtle.onkeypress(self.moveUp,"w")
        turtle.onkeypress(self.IssacMoveRight,"6")
        turtle.onkeypress(self.IssacMoveLeft,"4")
        turtle.done()
if __name__=="__main__":

    game= IssacNLilithGame()
    game.gameRun()